<?php

class ProfilesController extends Controller
{
  public $layout='//layouts/main';

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index', 'edit', 'view', 'update'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionEdit()
	{
    $user = Users::model()->find('id = :id', array(':id' => $_SESSION['id']));

    $member_id = $_SESSION['member_id'];
    $pageSize = 10;

    //News------------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    $news = News::model()->findAll($criteria);

    //Articles-------------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    $articles = Articles::model()->findAll($criteria);

    //Links-------------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    $links = Links::model()->findAll($criteria);

    //Journals-----------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    //Pagination
    $journals_pages = new CPagination(Journals::model() -> count());
    $journals_pages -> pageSize = $pageSize;
    $journals_pages -> applyLimit($criteria);

    $journals = Journals::model()->findAll($criteria);

    //Webinars-----------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    //Pagination
    $webinars_pages = new CPagination(Webinars::model() -> count());
    $webinars_pages -> pageSize = $pageSize;
    $webinars_pages -> applyLimit($criteria);

    $webinars = Webinars::model()->findAll($criteria);

    //Download URLs-----------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    //Pagination
    $downloadUrls_pages = new CPagination(DownloadUrls::model() -> count());
    $downloadUrls_pages -> pageSize = $pageSize;
    $downloadUrls_pages -> applyLimit($criteria);

    $downloadUrls = downloadUrls::model()->findAll($criteria);

		$this->render('edit',array(
      'user'=>$user,
      'news' => $news,
      'articles' => $articles,
      'links' => $links,
      'journals' => $journals,
      'journals_pages' => $journals_pages,
      'webinars' => $webinars,
      'webinars_pages' => $webinars_pages,
      'downloadUrls' => $downloadUrls,
      'downloadUrls_pages' => $downloadUrls_pages,
		));
	}

  public function actionUpdate()
  {
    if($_POST['Users'])
    {
      $data = $_POST['Users'];
      $user = Users::model()->findByPk($_SESSION['id']);

      $data['password'] = md5($data['password']);
      $tmp = $user->password;
      $user->attributes = $data;

      if($data['password'] == NULL)
      {
        $user->password = $tmp;
      }

      if($user->save())
      {
        $this->redirect(bu('members?notif=success&do=update'));
      }
    }

   $member_id = $_SESSION['member_id'];
    $pageSize = 10;

    //News------------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    $news = News::model()->findAll($criteria);

    //Articles-------------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    $articles = Articles::model()->findAll($criteria);

    //Links-------------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    $links = Links::model()->findAll($criteria);

    //Journals-----------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    //Pagination
    $journals_pages = new CPagination(Journals::model() -> count());
    $journals_pages -> pageSize = $pageSize;
    $journals_pages -> applyLimit($criteria);

    $journals = Journals::model()->findAll($criteria);

    //Webinars-----------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    //Pagination
    $webinars_pages = new CPagination(Webinars::model() -> count());
    $webinars_pages -> pageSize = $pageSize;
    $webinars_pages -> applyLimit($criteria);

    $webinars = Webinars::model()->findAll($criteria);

    //Download URLs-----------------------------------------
    $criteria = new CDbCriteria;
    $criteria -> order = 'created_at';
    $criteria -> condition = "member_id = ".$member_id;

    //Pagination
    $downloadUrls_pages = new CPagination(DownloadUrls::model() -> count());
    $downloadUrls_pages -> pageSize = $pageSize;
    $downloadUrls_pages -> applyLimit($criteria);

    $downloadUrls = downloadUrls::model()->findAll($criteria);

		$this->render('edit',array(
      'user'=>$user,
      'news' => $news,
      'articles' => $articles,
      'links' => $links,
      'journals' => $journals,
      'journals_pages' => $journals_pages,
      'webinars' => $webinars,
      'webinars_pages' => $webinars_pages,
      'downloadUrls' => $downloadUrls,
      'downloadUrls_pages' => $downloadUrls_pages,
		));
  }
}